{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6453d3d5",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/alephalpha.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "731d44db9e0d236c",
   "metadata": {},
   "source": [
    "# Aleph Alpha\n",
    "\n",
    "Aleph Alpha is a powerful language model that can generate human-like text. Aleph Alpha is capable of generating text in multiple languages and styles, and can be fine-tuned to generate text in specific domains."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c78b172f",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e31874a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-alephalpha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50fc1a30",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f2a12560669efa6",
   "metadata": {},
   "source": [
    "#### Set your Aleph Alpha token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85fbba23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"AA_TOKEN\"] = \"your_token_here\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b81a3ef6-2ee5-460d-9aa4-f73708774014",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "910b50ad-c55e-487e-8808-5905dfaa78b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.alephalpha import AlephAlpha\n",
    "\n",
    "# To customize your token, do this\n",
    "# otherwise it will lookup AA_TOKEN from your env variable\n",
    "# llm = AlephAlpha(token=\"<aa_token>\")\n",
    "llm = AlephAlpha(model=\"luminous-base-control\")\n",
    "\n",
    "resp = llm.complete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfda925e-89c5-47a6-9311-16916ab08b66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " a well-known computer scientist and entrepreneur. He is the co-founder of PayPal and a co-founder of the Y Combinator startup accelerator. He has also co-authored the book \"Programming the Web\". Paul Graham is also a frequent speaker and writer on topics related to computer science, entrepreneurship, and startups. He has written several blog posts on the topic of \"Why Startups Fail\". In this post, I will summarize some of the key points from Paul Graham's blog post on why startups fail.\n",
      "\n",
      "1. Lack of a clear vision: Startups often lack a clear vision of what they\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "413fbe0a60a8e449",
   "metadata": {},
   "source": [
    "#### Additional Response Details\n",
    "To access detailed response information such as log probabilities, ensure your AlephAlpha instance is initialized with the `log_probs` parameter. The `logprobs` attribute of the `CompletionResponse` will contain this data. Other details like the model version and raw completion text can be accessed directly if they're part of the response or via `additional_kwargs`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de81c4b2e2c36894",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Log Probabilities:\n",
      "Token:  a, LogProb: -0.95955\n",
      "Token:  well, LogProb: -1.9219251\n",
      "Token: -, LogProb: -0.1312752\n",
      "Token: known, LogProb: -0.022855662\n",
      "Token:  computer, LogProb: -0.9569155\n",
      "Token:  scientist, LogProb: -0.06721641\n",
      "Token:  and, LogProb: -0.56296504\n",
      "Token:  entrepreneur, LogProb: -0.65574974\n",
      "Token: ., LogProb: -0.5926046\n",
      "Token:  He, LogProb: -0.1885516\n",
      "Token:  is, LogProb: -0.3927348\n",
      "Token:  the, LogProb: -0.46820825\n",
      "Token:  co, LogProb: -0.465878\n",
      "Token: -, LogProb: -0.024082167\n",
      "Token: founder, LogProb: -0.009869587\n",
      "Token:  of, LogProb: -0.31641242\n",
      "Token:  PayPal, LogProb: -1.0825713\n",
      "Token:  and, LogProb: -0.39408743\n",
      "Token:  a, LogProb: -1.45493\n",
      "Token:  co, LogProb: -1.0837904\n",
      "Token: -, LogProb: -0.0011430404\n",
      "Token: founder, LogProb: -0.074010715\n",
      "Token:  of, LogProb: -0.038962167\n",
      "Token:  the, LogProb: -1.7761776\n",
      "Token:  Y, LogProb: -0.41853565\n",
      "Token:  Combin, LogProb: -0.17868777\n",
      "Token: ator, LogProb: -2.0265374e-05\n",
      "Token:  startup, LogProb: -0.24595682\n",
      "Token:  acceler, LogProb: -0.5855012\n",
      "Token: ator, LogProb: -6.675698e-06\n",
      "Token: ., LogProb: -0.022597663\n",
      "Token:  He, LogProb: -0.8310143\n",
      "Token:  has, LogProb: -1.5842702\n",
      "Token:  also, LogProb: -0.5774656\n",
      "Token:  been, LogProb: -1.3938092\n",
      "Token:  a, LogProb: -0.67207164\n",
      "Token:  professor, LogProb: -1.0511048\n",
      "Token:  at, LogProb: -0.13273911\n",
      "Token:  the, LogProb: -0.7993539\n",
      "Token:  MIT, LogProb: -1.2281163\n",
      "Token:  Media, LogProb: -0.7707413\n",
      "Token:  Lab, LogProb: -0.06716257\n",
      "Token: ., LogProb: -0.9140582\n",
      "Token:  Paul, LogProb: -0.8244309\n",
      "Token:  Graham, LogProb: -0.15202633\n",
      "Token:  has, LogProb: -1.3735206\n",
      "Token:  written, LogProb: -0.77148163\n",
      "Token:  several, LogProb: -0.7167357\n",
      "Token:  books, LogProb: -0.24542983\n",
      "Token:  on, LogProb: -0.77700675\n",
      "Token:  computer, LogProb: -0.8485363\n",
      "Token:  science, LogProb: -0.026196867\n",
      "Token:  and, LogProb: -0.4796574\n",
      "Token:  entrepreneurs, LogProb: -0.48952234\n",
      "Token: hip, LogProb: -1.0847986e-05\n",
      "Token: ,, LogProb: -0.1426171\n",
      "Token:  including, LogProb: -0.10799221\n",
      "Token:  \", LogProb: -0.4733107\n",
      "Token: Program, LogProb: -0.9295699\n",
      "Token: ming, LogProb: -0.00090034\n",
      "Token:  the, LogProb: -1.5219054\n",
      "Token:  Universe, LogProb: -1.2475122\n",
      "Token: \", LogProb: -0.8377396\n",
      "Token:  and, LogProb: -0.014596111\n",
      "Token:  \", LogProb: -0.0034322182\n",
      "Token: The, LogProb: -0.97810173\n",
      "Token:  Art, LogProb: -1.4708842\n",
      "Token:  of, LogProb: -0.0017665509\n",
      "Token:  Computer, LogProb: -0.027323013\n",
      "Token:  Programming, LogProb: -0.09090222\n",
      "Token: \"., LogProb: -0.2312944\n",
      "Token:  He, LogProb: -0.9431941\n",
      "Token:  is, LogProb: -0.52350885\n",
      "Token:  also, LogProb: -0.8409716\n",
      "Token:  the, LogProb: -1.2813272\n",
      "Token:  founder, LogProb: -0.8080497\n",
      "Token:  of, LogProb: -0.12735468\n",
      "Token:  the, LogProb: -0.26858208\n",
      "Token:  startup, LogProb: -1.7183943\n",
      "Token:  incub, LogProb: -0.71643037\n",
      "Token: ator, LogProb: -0.00013922676\n",
      "Token: ,, LogProb: -1.6374074\n",
      "Token:  Y, LogProb: -1.3464186\n",
      "Token:  Combin, LogProb: -0.043204635\n",
      "Token: ator, LogProb: -1.490105e-05\n",
      "Token: ., LogProb: -0.48073012\n",
      "Token: <|endoftext|>, LogProb: -0.30235213\n",
      "\n",
      "Model Version:\n",
      "20240215\n",
      "\n",
      "Raw Completion:\n",
      " a well-known computer scientist and entrepreneur. He is the co-founder of PayPal and a co-founder of the Y Combinator startup accelerator. He has also been a professor at the MIT Media Lab. Paul Graham has written several books on computer science and entrepreneurship, including \"Programming the Universe\" and \"The Art of Computer Programming\". He is also the founder of the startup incubator, Y Combinator.\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.alephalpha import AlephAlpha\n",
    "\n",
    "llm = AlephAlpha(model=\"luminous-base-control\", log_probs=0)\n",
    "\n",
    "resp = llm.complete(\"Paul Graham is \")\n",
    "\n",
    "if resp.logprobs is not None:\n",
    "    print(\"\\nLog Probabilities:\")\n",
    "    for lp_list in resp.logprobs:\n",
    "        for lp in lp_list:\n",
    "            print(f\"Token: {lp.token}, LogProb: {lp.logprob}\")\n",
    "\n",
    "if \"model_version\" in resp.additional_kwargs:\n",
    "    print(\"\\nModel Version:\")\n",
    "    print(resp.additional_kwargs[\"model_version\"])\n",
    "\n",
    "if \"raw_completion\" in resp.additional_kwargs:\n",
    "    print(\"\\nRaw Completion:\")\n",
    "    print(resp.additional_kwargs[\"raw_completion\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5152a2b4-78e6-47a5-933d-f5186ec0f775",
   "metadata": {},
   "source": [
    "## Async"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7df04c0e-43ee-4176-9aad-94781d0ed36d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.alephalpha import AlephAlpha\n",
    "\n",
    "llm = AlephAlpha(model=\"luminous-base-control\")\n",
    "resp = await llm.acomplete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be09c52b-604a-4f05-8f93-36e6ea882ff5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " a computer scientist and entrepreneur who is known for his work in the field of artificial intelligence and computer science. He is the co-founder of the company Y Combinator, which is a startup accelerator that helps startups get funding and resources. Paul Graham has also written several books on computer science and entrepreneurship, including \"Programming: Principles and Practice\" and \"The Art of Computer Programming\". He is a well-known figure in the computer science community and has made significant contributions to the field.\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
